﻿/**********************************************************************************
 * Description:     
 * MemberVariable:
 * Functions:       
 * Modify information:     
 * 2008-8-5     New           DuYaoHui     Create Class
 * 
 * ********************************************************************************/
using System;
using System.Data;
using System.Web.UI.WebControls;
using Fjaspx.Sys.Bll;
using Fjaspx.Common;

namespace Fjaspx.UI.Web
{
    public partial class Administrator_caiye_admin : AdminPage
    {
        private int _pages;
        public int pages
        {
            set { _pages = value; }
            get { return _pages; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                SystemCms.AdminSesion();
                try
                {
                Repeater_admin_bind(DdlMenu.SelectedValue.Trim());
                //DropDownList_class_bind();
                DdlMenu_bind();

                switch (Request["t"])
                {
                    case "del":
                        del_news(Convert.ToInt32(Request["id"]));
                        Repeater_admin_bind(DdlMenu.SelectedValue.Trim());
                        break;
                    case "ding":
                        if (Request["ding"] == "1") { Ding_news(Convert.ToInt32(Request["id"]), 0); }
                        else { Ding_news(Convert.ToInt32(Request["id"]), 1); }
                        Repeater_admin_bind(DdlMenu.SelectedValue.Trim());
                        break;
                    case "tuijian":
                        if (Request["tuijian"] == "1") { Tuijian_news(Convert.ToInt32(Request["id"]), 0); }
                        else { Tuijian_news(Convert.ToInt32(Request["id"]), 1); }
                        Repeater_admin_bind(DdlMenu.SelectedValue.Trim());
                        break;
                    case "K":
                        if (Request["K"] == "0") { K_news(Convert.ToInt32(Request["id"]), 1); }
                        else { K_news(Convert.ToInt32(Request["id"]), 0); }
                        Repeater_admin_bind(DdlMenu.SelectedValue.Trim());
                        break;
                }

                }
                catch { Response.Redirect("/administrator/Message.aspx?message=数据绑定错误！&z=no");
                Fjaspx.Common.SystemCms.InsertErrLog("数据绑定错误！", Request.PhysicalPath);
                }
            }
        }
        void Repeater_admin_bind(string classs)
        {

            int page = 0;
            string where = "";
            if (Request["page"] != null)
            {
                page = Convert.ToInt32(Request["page"]) - 1;
            }

            if (Request["keywords"] != null)
            {
                where = " title =" + Request["keywords"].Trim();
            }

            if (classs != "")
            {
                where = " classname =" + classs;
            }

            //if (Request["orderby"] != "")
            //{
            //    
            //}
            //where += " order by bh desc ";


            if (Fjaspx.Common.Session.Get("user") != null)
            {
                if (where != "")
                {
                    where = where + " and author = '" + Fjaspx.Common.Session.Get("user") + "' and type='caiye' ";
                }
                else { where = " author = '" + Fjaspx.Common.Session.Get("user") + "' and type='caiye' "; }
            }
            else
            {
                if (where != "")
                {
                    where = where + "  and type='caiye' ";
                }
                else { where = " type='caiye' "; }
            }


            ROYcms_news Bll = new ROYcms_news();
            pages = Bll.GetCount(where) / 18 + 1; //分页的总数          
            PagedDataSource pg = new PagedDataSource();
            pg.DataSource = Bll.GetList(where).Tables[0].DefaultView;
            pg.AllowPaging = true;
            pg.PageSize = 18;
            pg.CurrentPageIndex = page;
            Repeater_admin.DataSource = pg;
            Repeater_admin.DataBind();


        }


        #region 数据绑定
        void DdlMenu_bind()
        {

            //this.btnAdd.Attributes.Add("onclick", "return ChkInput()");
            //this.btnEdit.Attributes.Add("onclick", "return ChkInput()");

            DataSet ds = new Fjaspx.Sys.Bll.ROYcms_class().GetClassList(250);

            //this.rptMenuList.DataSource = ds.Tables[0].DefaultView;
            //this.rptMenuList.DataBind();

            this.DdlMenu.Items.Clear();
            this.DdlMenu.Items.Add(new ListItem("请选择所属分类", ""));
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                int ClassTj = Convert.ToInt32(dr["ClassTj"]);
                string ClassId = dr["Id"].ToString().Trim();
                string ClassName = dr["ClassName"].ToString().Trim();

                if (ClassTj == 1)
                {
                    this.DdlMenu.Items.Add(new ListItem(ClassName, ClassId));

                }
                else
                {
                    ClassName = "├ " + ClassName;
                    ClassName = Fjaspx.Common.StringPlus.StringOfChar(ClassTj - 1, "　") + ClassName;

                    this.DdlMenu.Items.Add(new System.Web.UI.WebControls.ListItem(ClassName, ClassId));
                }
            }

        }
        #endregion

        void del_news(int bh)
        {

            ROYcms_news BLL = new ROYcms_news();
            BLL.Delete(bh);

            //删除静态生成文件
            //if (System.Configuration.ConfigurationManager.AppSettings["HTML"] == "true")
            //{
            //    Fjaspx.Sys.Model.ROYcms_news Model = BLL.GetModel(bh);
            //    StringBuilder path = new StringBuilder();
            //    path.Append("~/HTML/");
            //    path.Append(Model.time.Value.Year.ToString());
            //    path.Append("/");
            //    path.Append(Model.time.Value.Month.ToString());
            //    path.Append("/");
            //    path.Append(bh.ToString());
            //    path.Append(".html");
            //    SystemCms.DeleteFile(Server.MapPath(path.ToString()));
            //}
        }
        //  控制开关 打开 和关闭一条数据
        void K_news(int bh, int on_of)
        {
            ROYcms_news BLL = new ROYcms_news();
            BLL.K_news(bh, on_of);
        }
        //修改一条数据是否置顶  推荐
        void Ding_news(int bh, int ding)
        {

            ROYcms_news BLL = new ROYcms_news();
            BLL.Ding_news(bh, ding);
        }
        void Tuijian_news(int bh, int tuijian)
        {

            ROYcms_news BLL = new ROYcms_news();
            BLL.Tuijian_news(bh, tuijian);
        }
        protected void ImageButton_all_del_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //批量删除
            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    del_news(id);
                    nub += 1;
                }
            }
            Response.Redirect("/administrator/Message.aspx?message=批量删除<b>" + nub + "</b>条信息成功！&z=yes");
        }
        protected void ImageButton_ding_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //批量推荐
            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    Tuijian_news(id, 0);
                    nub += 1;
                }
            }
            Response.Redirect("/administrator/Message.aspx?message=批量推荐<b>" + nub + "</b>条信息成功！&z=yes");
        }
        protected void ImageButton_K_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //批量验证
            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    K_news(id, 0);
                    nub += 1;
                }
            }
            Response.Redirect("/administrator/Message.aspx?message=批量验证<b>" + nub + "</b>条信息成功！&z=yes");
        }

        protected void DdlMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DdlMenu.SelectedValue != "")
            {
                Repeater_admin_bind(DdlMenu.SelectedValue.Trim());
            }

        }

    }
}